package com.qcu.controller.staff;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qcu.annotation.LimitRate;
import com.qcu.annotation.RequireAuthorization;
import com.qcu.entity.Appointment;
import com.qcu.service.AppointmentService;
import com.qcu.util.Response;
import com.qcu.util.ResponseCode;
import com.qcu.util.ThreadLocalUtil;
import com.qcu.vo.AppointmentInfoTDVO;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * @Name: MyComputer
 * @ClassName AppointmentController
 * @Description:
 * TODO：在进行操作前需要进行用户确认
 * @date 2025/2/11 15:52
 */
@RestController
@RequestMapping("/staff/appointment")
public class StaffAppointmentController {

    @Resource
    private AppointmentService appointmentService;
    @Value("${spring.mail.username}")
    private String from;
    @Resource
    JavaMailSender javaMailSender;

    @GetMapping("/get")
    @LimitRate(key = "getAppointmentInfo",permitsPerSecond = 2,expire = 10,msg = "八嘎雅鹿，你滴点那么快滴干什么滴干活...")
    @RequireAuthorization({"STAFF"})
    public Response get() {
        List<AppointmentInfoTDVO> info = appointmentService.getAppointmentInfo();
        return Response.success(info);
    }
    @PutMapping("/accept/{id}")
    @RequireAuthorization({"STAFF"})
    public Response accept(@PathVariable String id) {
        // todo : 判断是否属于当前用户的预约
        LambdaUpdateWrapper<Appointment> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Appointment::getId, id).set(Appointment::getState, 1);
        boolean update = appointmentService.update(wrapper);
        Appointment one = appointmentService.getById(id);
        // 发送邮件
        SimpleMailMessage message = new SimpleMailMessage();
        if (update){
            message.setFrom(from);
            message.setTo(one.getEmail());
            message.setSubject("预约结果通知");
            message.setText("您的预约已经通过审核，按约定时间前往");
            javaMailSender.send(message);
            return Response.success();
        }
        return Response.failure(ResponseCode.FAILURE);
    }

    @PutMapping("/complete/{id}")
    @RequireAuthorization({"STAFF"})
    public Response complete(@PathVariable String id,@RequestBody String result) {
        LambdaUpdateWrapper<Appointment> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Appointment::getId, id).set(Appointment::getState, 2).set(Appointment::getResult,result).set(Appointment::getAgreeTime, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        boolean update = appointmentService.update(wrapper);
        if (update)
            return Response.success();
        return Response.failure(ResponseCode.FAILURE);
    }

    /**
     * 查看结果
     * @param AID
     * @return
     */
    @GetMapping("/getAIOD/{AID}")
    @LimitRate(key = "getAppointmentInfoOfDoctor",permitsPerSecond = 2,expire = 10,msg = "八嘎雅鹿，你滴点那么快滴干什么滴干活...")
    @RequireAuthorization({"STAFF"})
    public Response getAppointmentInfoOfDoctor(@PathVariable String AID) {
        Map<String,Integer> map = ThreadLocalUtil.get();
        Integer id = map.get("id");
        LambdaQueryWrapper<Appointment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Appointment::getDoctorId,id).eq(Appointment::getId,AID);
        Appointment one = appointmentService.getOne(wrapper);
        return Response.success(one);
    }
}
